package com.sgb.kjwl.view.ui.activity.self.pick;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityLaborPriceListBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.user.PriceListEntity;
import com.sgb.kjwl.model.entity.user.UserLaborEntity;
import com.sgb.kjwl.view.ui.adapter.self.pick.LaborPriceListAdapter;
import com.sgb.kjwl.viewmodel.self.pick.LaborPriceListModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.refresh.SuperSwipeRefreshLayout;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

public class LaborPriceListActivity extends AppActivity {
    private ActivityLaborPriceListBinding binding;
    @Inject
    LaborPriceListModel viewModel;

    private LaborPriceListAdapter laborPriceListAdapter;
    private String no;
    public  String sort = "down";

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    public static void start(Context context, UserLaborEntity userLaborEntity) {
        Intent intent = new Intent();
        intent.putExtra("no",userLaborEntity.getNo());
        intent.putExtra("projName",userLaborEntity.getProjName());
        intent.putExtra("type",userLaborEntity.getType());
        intent.putExtra("subject",userLaborEntity.getSubject());
        intent.putExtra("unit",userLaborEntity.getUnit());
        intent.putExtra("num",userLaborEntity.getNum());
        intent.setClass(context, LaborPriceListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        no = getIntent().getStringExtra("no");
        String projName = getIntent().getStringExtra("projName");
        String type = getIntent().getStringExtra("type");
        String subject = getIntent().getStringExtra("subject");
        String unit = getIntent().getStringExtra("unit");
        String num = getIntent().getStringExtra("num");
        binding = DataBindingUtil.setContentView(LaborPriceListActivity.this, R.layout.activity_labor_price_list);
        binding.include.titleView.setText("比价单(劳务)");
        binding.tvProjectName.setText(projName);
        binding.tvEngineeringName.setText(type);
        binding.tvSubItemName.setText(subject);
        binding.tvNum.setText(num);
        binding.tvUnit.setText(unit);
        binding.setViewModel(viewModel);
    }

    @Override
    protected void init() {
        binding.include.ivBack.setOnClickListener(this);
        binding.llSort.setOnClickListener(this);
        if (laborPriceListAdapter == null) {
            laborPriceListAdapter  = new LaborPriceListAdapter(LaborPriceListActivity.this, null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(LaborPriceListActivity.this);
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(laborPriceListAdapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getData(no, sort);
                binding.recyclerView.setLoadingMoreEnabled(true);
            }

            @Override
            public void onLoadMore() {
                if (laborPriceListAdapter.getDatas().size() < viewModel.total){
                    viewModel.getMoreData(no, sort);
                }else {
                    binding.recyclerView.loadMoreComplete();
                }
            }
        });

        viewModel.getData(no, sort);

        binding.recyclerView.setPullRefreshEnabled(false);

        binding.swipeRefresh.setHeaderView(createHeaderView());
        binding.swipeRefresh.setOnPullRefreshListener(new SuperSwipeRefreshLayout.OnPullRefreshListener() {
            @Override
            public void onRefresh() {
                textView.setText("正在刷新");
                viewModel.getData(no,sort);
            }

            @Override
            public void onPullDistance(int distance) {

            }

            @Override
            public void onPullEnable(boolean enable) {
                textView.setText(enable ? "释放立即刷新" : "下拉刷新");
            }
        });
    }

    // Header View
    private ProgressBar progressBar;
    private TextView textView;

    private View createHeaderView() {
        View headerView = LayoutInflater.from(binding.swipeRefresh.getContext())
                .inflate(R.layout.super_swipe_refresh_head, null);
        progressBar = (ProgressBar) headerView.findViewById(R.id.pb_view);
        textView = (TextView) headerView.findViewById(R.id.text_view);
        textView.setText("下拉刷新");
        progressBar.setVisibility(View.GONE);
        return headerView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_sort:
                if ("down".equals(sort)){
                    sort = "up";
                }else {
                    sort = "down";
                }
                viewModel.getData(no, sort);
                break;
        }
    }

    public void showData(List<PriceListEntity> list){
        binding.recyclerView.refreshComplete();
        binding.swipeRefresh.setRefreshing(false);
        if (list.size() == 0){
            binding.recyclerView.setVisibility(View.GONE);
            binding.emptyView.setVisibility(View.VISIBLE);
        }else {
            binding.recyclerView.setVisibility(View.VISIBLE);
            binding.emptyView.setVisibility(View.GONE);
            laborPriceListAdapter.setDatas(list);
        }
    }

    public void showMoreData(List<PriceListEntity> list){
        binding.recyclerView.loadMoreComplete();
        binding.emptyView.setVisibility(View.GONE);
        laborPriceListAdapter.addDatas(list);
    }
}
